﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Radovici.Ctrls;
using Radovici.SharedLibrary;
using Radovici.ThemesManager;
using MessageBox=Radovici.Ctrls.MessageBox;

namespace YouTubePlaylist
{
    using YouTubeService;
    public partial class PlaylistsCtrl : UserControl, IPlaylistsCtrl
    {
        public event MouseButtonEventHandler MouseDoubleClicked;

        private PlaylistsViewModel _viewModel;

        public PlaylistsCtrl()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(OnLoaded);
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {            
            Options.ApplyTheme("RainierOrange.xaml");
            this.ApplyTheme();
        }

        public PlaylistsViewModel ViewModel
        {
            get { return _viewModel; }
            private set
            {
                _viewModel = value;
                var element = this.Content as FrameworkElement;
                if (element != null)
                {
                    Dispatcher.BeginInvoke(delegate { element.DataContext = _viewModel; });
                }
            }
        }

        public PlaylistsViewModel PlaylistsViewModel
        {
            get { return (PlaylistsViewModel)GetValue(PlaylistsProperty); }
            set { SetValue(PlaylistsProperty, value); }
        }

        public static readonly DependencyProperty PlaylistsProperty =
            DependencyProperty.Register("PlaylistsViewModel", typeof(PlaylistsViewModel), typeof(PlaylistsCtrl), new PropertyMetadata(null, onPlaylistsChanged));

        private static void onPlaylistsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PlaylistsCtrl ctrl = d as PlaylistsCtrl;
            if (ctrl != null)
            {
                ctrl.ViewModel = e.NewValue as PlaylistsViewModel;
            }
        }

        private void onAddPlaylist(object sender, RoutedEventArgs e)
        {
            new PlaylistInputCtrl(new Playlist()).Show("New Playlist", OnPlaylistChanged, sender as UIElement);
        }

        #region TODO: Common PlaylistsViewModel code
        private void OnEdit(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                Playlist p = ViewModel.SelectedPlaylist;
                if (p != null)
                {
                    new PlaylistInputCtrl(p).Show("Edit Playlist", OnPlaylistChanged, sender as UIElement);
                }
            }
        }

        private void OnPlaylistChanged(object ctrl, EventArgs e)
        {
            var inputCtrl = ctrl as PlaylistInputCtrl;
            if (inputCtrl != null)
            {
                var playlist = inputCtrl.DataContext as Playlist;
                if (playlist != null)
                {
                    _viewModel.SavePlaylist(playlist);
                }
            }
        }

        private void OnDeletePlaylist(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                var dlg = new Radovici.Ctrls.MessageBox();
                dlg.Message = string.Format("Are you sure you want to delete {0}?", ViewModel.SelectedPlaylist.Name);
                dlg.Show("Delete Playlist", OnDeletePlaylistConfirmed, btn);                
            }
        }

        private void OnDeletePlaylistConfirmed(object sender, EventArgs e)
        {
            _viewModel.DeletePlaylist(ViewModel.SelectedPlaylist);
        }

        private void onDoubleClicked(object sender, MouseButtonEventArgs e)
        {
            var data = sender as DataGrid;
            if (data != null)
            {
                Playlist p = data.SelectedItem as Playlist;
                if (p != null)
                {
                    _viewModel.Current = ViewModel.PlaylistsViewModels.Single(vm => vm.Playlist.Id == p.Id);
                    if (MouseDoubleClicked != null)
                    {
                        MouseDoubleClicked(this, e);
                    }
                }
            }
        }

        private void OnPlaylistSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var grid = sender as DataGridPlus;
            if (grid != null)
            {
                ViewModel.SelectedPlaylist = grid.SelectedItem as Playlist;
            }
        }

        private void onMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var grid = sender as DataGridPlus;
            if (grid != null)
            {
                ViewModel.SelectedPlaylist = grid.SelectedItem as Playlist;
            }
        }

        private void OnCopyPlaylist(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                _viewModel.CopyPlaylist(ViewModel.SelectedPlaylist);
            }
        }

        private void OnCategorizePlaylist(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                Playlist p = ViewModel.SelectedPlaylist;
                if (p != null)
                {
                    new PlaylistCategoryCtrl(p).Show("Categorize Playlist", onPlaylistCategorySelected, sender as UIElement);
                }                
            }            
        }

        private void onPlaylistCategorySelected(object ctrl, EventArgs e)
        {
            var myCtrl = ctrl as PlaylistCategoryCtrl;
            if (myCtrl != null)
            {
                var playlist = myCtrl.Playlist;
                if (playlist != null)
                {
                    _viewModel.CategorizePlaylist(playlist, myCtrl.Category.Text);
                }
            }
        }

        private void OnLink(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                Playlist p = ViewModel.SelectedPlaylist;
                if (p != null)
                {
                    Clipboard.SetText(MailClient.GetLink(p));
                }
            }
        }

        private void OnMail(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                Playlist p = ViewModel.SelectedPlaylist;
                if (p != null)
                {
                    new MailLinkInputCtrl(ViewModel.Model.User, p, null).Show("Email a Friend", OnMailSent, sender as UIElement);
                }
            }
        }

        private void OnMailSent(object ctrl, EventArgs e)
        {
            var myCtrl = ctrl as MailLinkInputCtrl;
            if (myCtrl != null)
            {
                MailClient.SendMail(myCtrl.From.Text, myCtrl.To.Text, myCtrl.Subject.Text, myCtrl.Body);
            }
        }

        private void OnOptionMouseEnter(object sender, MouseEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                string option = btn.Tag as string;
                if (option != null)
                {
                    ViewModel.SelectedOption = option;
                }
            }
        }

        private void OnOptionMouseLeave(object sender, MouseEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                string option = btn.Tag as string;
                if (option != null)
                {
                    if (ViewModel.SelectedOption == option)
                    {
                        ViewModel.SelectedOption = null;
                    }
                }
            }
        }

        private void OnOptionsMouseMove(object sender, MouseEventArgs e)
        {
            ViewModel.IsActive = true;
        }
        #endregion
    }
}
